class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        int n = temperatures.size();
        vector<int> ans(n, 0);
        stack<int> s1;
        for (int i = 0; i < n; i++)
        {
            while (!s1.empty() && temperatures[i] > temperatures[s1.top()])
            {
                int temp = s1.top();
                ans[temp] = i - temp;
                s1.pop();
            }
            s1.push(i);
        }
        return ans;
    }
};